//
//  ForgetPasswordVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/26.
//  Copyright © 2016年 阳光. All rights reserved.
//  用户忘记密码或者用户名进行重置密码界面

#import "ForgetPasswordVC.h"
#import "LoginVC.h"

@interface ForgetPasswordVC ()<CustomNavViewDelegate>

@property(nonatomic,strong)LeftTitle_TextFieldView *resetPdView;

@property(nonatomic,strong)LeftTitle_TextFieldView *againPdView;

@end



@implementation ForgetPasswordVC

-(void)rightBtnEvent
{
    
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor=WhiteColor;
    
//    self.title=@"找回密码";
    
    [self initMainView];
    
}

-(void)leftBtnEvent
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)initMainView
{
    CustomNavView *registerNavView=[[CustomNavView alloc]initWithTitle:NSLocalizedString(@"Change Password", nil) leftBtnImage:@"back_gray" hasLeftBtnImage:YES rightBtnTitle:nil hasRightBtnTitle:NO];//@"找回密码"
    registerNavView.delegate=self;
    registerNavView.frame=CGRectMake(0,0,APPW,64);
    [self.view addSubview:registerNavView];
    
    UIView *topLine=[UIView new];
    [self.view addSubview:topLine];
    topLine.backgroundColor=MAIN_BACK_COLOR;
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.top.mas_equalTo(64);
    }];
    
    // 新密码
    {
        _resetPdView=[[LeftTitle_TextFieldView alloc]initWithTitle:NSLocalizedString(@"NewPassWord", nil) placeholder:NSLocalizedString(@"EnterNewPassword", nil)];//initWithTitle:@"新密码" placeholder:@"请输入新密码"];

        ViewBorderRadius(_resetPdView,kmargin/2,.5,ColorBDBDBD);
        [self.view addSubview:_resetPdView];
        _resetPdView.textField.secureTextEntry=YES;
        [_resetPdView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(kmargin*2+1+64);
            make.left.mas_equalTo(kmargin*3);
            make.size.mas_equalTo(CGSizeMake(APPW-kmargin*6,40));
        }];
    }
    
    // 确认密码
    {
        _againPdView=[[LeftTitle_TextFieldView alloc]initWithTitle:NSLocalizedString(@"SurePassword", nil) placeholder:NSLocalizedString(@"EnterPassword", nil)];//initWithTitle:@"确认密码" placeholder:@"请输入新密码"];

        ViewBorderRadius(_againPdView,kmargin/2,.5,ColorBDBDBD);
        [self.view addSubview:_againPdView];
        _againPdView.textField.secureTextEntry=YES;
        [_againPdView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.resetPdView.mas_bottom).offset(kmargin);
            make.left.mas_equalTo(kmargin*3);
            make.size.mas_equalTo(CGSizeMake(APPW-kmargin*6,40));
        }];
    }
    
    // 完成
    UIButton *OKBtn=[UIButton new];
    [self.view addSubview:OKBtn];
    OKBtn.backgroundColor=Main_Color;
    OKBtn.titleLabel.font=Font_Number(16);
    ViewBorderRadius(OKBtn,kmargin/2,1,ClearColor);
    [OKBtn setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];//@"完成"
    [OKBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [OKBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.againPdView.mas_bottom).offset(kmargin*2);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*6,40));
        make.left.mas_equalTo(kmargin*3);
    }];
    [OKBtn addTarget:self action:@selector(OKBtnEvent) forControlEvents:UIControlEventTouchUpInside];
}

// 完成
-(void)OKBtnEvent
{
    NSDictionary *dic= @{
                         @"doctor_id":_infoDic[@"id"],
                         @"password":_resetPdView.textField.text,
                         @"repassword":_againPdView.textField.text,
                         @"username":_infoDic[@"username"],
                         @"access_token":_infoDic[@"access_token"]
                         };

    __weak typeof(self) weakSelf=self;
    
    // 找回密码进行重置
    [QBRequest ModifyPdWithDic:dic Success:^(id res)
     {
         
         [self.view makeToast:res[@"message"] duration:1.0 position:CSToastPositionCenter];
         
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             
             // 跳转到登录界面
             LoginVC *loginVC=[LoginVC new];
           [weakSelf presentViewController:loginVC animated:NO completion:nil];
         });
         
         
     } failure:^(NSString *error) {
         
     }];
    
}

@end
